package com.hqyj.finance.server.users.financeusers.controller;

import com.hqyj.finance.server.users.financeusers.entity.Roles;
import com.hqyj.finance.server.users.financeusers.entity.UsersRoles;
import com.hqyj.finance.server.users.financeusers.json.R;
import com.hqyj.finance.server.users.financeusers.service.RolesService;
import com.hqyj.finance.server.users.financeusers.service.UsersRolesService;
import com.hqyj.finance.server.users.financeusers.service.UsersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/roles")
public class RolesController {


    @Resource
    private RolesService rolesService;

    @Resource
    private UsersRolesService usersRolesService;

    @Resource
    private UsersService usersService;
    @GetMapping("/")
    public R queryAllRolesList() {
        try {
            List<Roles> rolesList = rolesService.list(null);
            return R.success("查询角色列表集合成功！", rolesList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常!");
        }
    }

    @PostMapping("/update")
    public R updateUserRolesList(@RequestParam Integer uid, @RequestParam Integer flag, @RequestBody List<UsersRoles> usersRolesList) {
        try {
            System.out.println("更新用户角色列表....");
            System.out.println("用户编号：" + uid);
            System.out.println(usersRolesList);
            //return R.success("更新用户角色信息成功！");
            //首先更新用户的删除标志
            if(usersService.updateUserDeleteFlagByUid(uid,flag)) {
                if (usersRolesService.updateUserRolesListByUid(uid, usersRolesList)) {
                    return R.success("更新用户角色信息成功！");
                }
            }else{
                return R.fail("更新用户角色信息失败！");
            }
            return R.fail("更新用户角色信息失败！");

        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }
}
